﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Data;
using System.Collections;
using Microsoft.Reporting.WinForms;
using System.Reflection;

namespace WMS.HelperClasses
{
	public class FormsHelper
	{
		//public static void LoadCollection(IEntityCollection2 collection,  EntityField2 valueMember, EntityField2 displayMember, EntityField2 codeField)
		//{
		//    Business.GetLookup(collection, valueMember, codeField, displayMember);
		//    list.DataSource = collection;
		//    list.DisplayMember = displayMember.Name;
		//    list.ValueMember = valueMember.Name;
		//}
		public static void LoadComboBox(ComboBox list, EntityField2 valueMember, EntityField2 displayMember, Func<EntityField2, EntityField2, DataTable> dataRetrievalFunc)
		{
			list.DataSource = dataRetrievalFunc(valueMember, displayMember);
			list.DisplayMember = displayMember.Name;
			list.ValueMember = valueMember.Name;
		}

		public static void LoadComboBox(ComboBox list, EntityField2 valueMember, EntityField2 displayMember, Func<DataTable> dataRetrievalFunc)
		{
			list.DataSource = dataRetrievalFunc();
			list.DisplayMember = displayMember.Name;
			list.ValueMember = valueMember.Name;
		}

		public static int GetSelectedId(ComboBox comboBox)
		{
			return (int)((System.Data.DataRowView)(comboBox.SelectedItem)).Row.ItemArray[0];
		}
		public static string GetSelectedName(ComboBox comboBox)
		{
			return (string)((System.Data.DataRowView)(comboBox.SelectedItem)).Row.ItemArray[1];
		}

		public static void CheckProjects(ICollection projects, List<Control> controlsToDisable, Label projectsLbl)
		{
			var projectsCount = projects.Count;
			if (projectsCount <= 0)
			{
				foreach (var control in controlsToDisable)
				{
					control.Enabled = false;
				}
			}

			if (projectsCount <= 0)
			{
				projectsLbl.Visible = true;
			}
		}

		public static void CheckStoresAndProjects(ICollection stores, ICollection projects, List<Control> controlsToDisable, Label storesLbl, Label projectsLbl)
		{
			var storesCount = stores.Count;//((DataTable)storesDDL.DataSource).Rows.Count;
			var projectsCount = projects.Count;//((DataTable)projectsDDL.DataSource).Rows.Count;
			if (storesCount <= 0 || projectsCount <= 0)
			{
				foreach (var control in controlsToDisable)
				{
					control.Enabled = false;
				}
			}

			if (projectsCount <= 0)
			{
				projectsLbl.Visible = true;
			}
			if (storesCount <= 0)
			{
				storesLbl.Visible = true;
			}
		}

		public static DataGridViewCellStyle GetGridColumnHeaderCellStyle()
		{
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
			dataGridViewCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle.BackColor = System.Drawing.SystemColors.ActiveCaption;
			dataGridViewCellStyle.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			dataGridViewCellStyle.ForeColor = System.Drawing.Color.White;
			dataGridViewCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;
			dataGridViewCellStyle.SelectionForeColor = System.Drawing.Color.Black;
			dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
			return dataGridViewCellStyle;
		}
		public static DataGridViewCellStyle GetGridCellStyle()
		{
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
			dataGridViewCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle.BackColor = System.Drawing.SystemColors.Window;
			dataGridViewCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			dataGridViewCellStyle.ForeColor = System.Drawing.SystemColors.ControlText;
			dataGridViewCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
			dataGridViewCellStyle.SelectionForeColor = System.Drawing.Color.Black;
			dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.False;

			return dataGridViewCellStyle;
		}

		public static DataGridViewCellStyle GetGridRowHeaderCellStyle()
		{
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle = new System.Windows.Forms.DataGridViewCellStyle();

			dataGridViewCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle.BackColor = System.Drawing.SystemColors.ActiveCaption;
			dataGridViewCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			dataGridViewCellStyle.ForeColor = System.Drawing.Color.White;
			dataGridViewCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
			dataGridViewCellStyle.SelectionForeColor = System.Drawing.Color.Black;
			dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;

			return dataGridViewCellStyle;

		}

		public static void SetGridDefaultStyles(DataGridView grid)
		{
			grid.BackgroundColor = System.Drawing.Color.White;
			grid.BorderStyle = System.Windows.Forms.BorderStyle.None;
			grid.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
			grid.ColumnHeadersHeight = 22;
			grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			grid.EnableHeadersVisualStyles = false;
			grid.ColumnHeadersDefaultCellStyle = HelperClasses.FormsHelper.GetGridColumnHeaderCellStyle();
			grid.DefaultCellStyle = HelperClasses.FormsHelper.GetGridCellStyle();
			grid.RowHeadersDefaultCellStyle = HelperClasses.FormsHelper.GetGridRowHeaderCellStyle();
		}

		public void DisableReportExportToWord(ReportViewer reportControl)
		{
			var control = (ToolStripDropDownButton)((ToolStrip)reportControl.Controls[0].Controls[0].Controls[2].Controls[1].Controls[0]).Items[15];			
			control.DropDownOpening += new EventHandler(control_DropDownOpening);

			Control reportPanel = reportControl.Controls[0].Controls[0].Controls[2].Controls[0].Controls[0].Controls[2].Controls[0].Controls[1];
			System.Reflection.FieldInfo prop2 = reportPanel.GetType().GetField("m_contextMenu", BindingFlags.NonPublic | BindingFlags.Instance);
			if (null != prop2)
			{
				((ContextMenuStrip)prop2.GetValue(reportPanel)).Items.RemoveAt(6);
			}
			//control.ToolTipText = "Walaa";
			//CountControls(reportControl);			
		}


		List<string> names = new List<string>();
		List<Control> controls = new List<Control>();

		void CountControls(Control control)
		{
			try
			{
				foreach (Control childControl in control.Controls)
				{
					PropertyInfo prop = childControl.GetType().GetProperty("ShowContextMenu", BindingFlags.Public | BindingFlags.Instance);
					if (null != prop && prop.CanWrite)
					{
						prop.SetValue(childControl, false, null);
					}
					System.Reflection.FieldInfo prop2 = childControl.GetType().GetField("m_contextMenu", BindingFlags.NonPublic | BindingFlags.Instance);
					if (null != prop2)
					{ 
						//prop2.SetValue(childControl, null);
						((ContextMenuStrip)prop2.GetValue(childControl)).Items.RemoveAt(6);
					}
					System.Reflection.FieldInfo field2 = childControl.GetType().GetField("m_showContextMenu", BindingFlags.NonPublic | BindingFlags.Instance);
					if (null != field2)
					{
						field2.SetValue(childControl, false);
					}

					if (names.Count == 19)
					{
						//((Panel)childControl).ShowContextMenu = false;
					}
					names.Add(childControl.Name);
					controls.Add(childControl);
					CountControls(childControl);
				}
			}
			catch (Exception ex)
			{
				var z = ex.Message;
			}
		}

		void control_DropDownOpening(object sender, EventArgs e)
		{
			((System.Windows.Forms.ToolStripDropDownItem)(sender)).DropDownItems.RemoveAt(2);
		}

		public static bool IsNumericCharachter(char keyChar)
		{

			if ((keyChar >= 48 && keyChar <= 57) || keyChar == 8)
			{
				return true;
			}

			return false;
		}

		public static void HandleNumericTextBoxKeypress(object sender, KeyPressEventArgs e)
		{ 
			HandleNumericTextBoxKeypress(sender, e, 2);
		}
		public static void HandleNumericTextBoxKeypress(object sender, KeyPressEventArgs e, short decimalPlaces)
		{
			if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
			{
				e.Handled = true;
			}

			// only allow one decimal point
			TextBox textBox = (TextBox)sender;

			if (e.KeyChar == '.' && textBox.Text.IndexOf('.') > -1)
			{
				e.Handled = true;
			}

			if (!char.IsControl(e.KeyChar))
			{				
				if (textBox.Text.IndexOf('.') > -1 
					&& textBox.Text.Substring(textBox.Text.IndexOf('.')).Length > decimalPlaces)
				{
					e.Handled = true;
				}
			}
		}

		public static bool ValidatePermitNumber(Form form, TextBox permitTextBox, List<ErrorProvider> errorProviders, string transactionType)
		{ 
			if (!string.IsNullOrEmpty(permitTextBox.Text.Trim()))
			{
				if (Business.CheckExisitngPermitNumber(permitTextBox.Text.Trim(), transactionType))
				{
					var errorProvider = new ErrorProvider(form);
					errorProviders.Add(errorProvider);

					errorProvider.SetError(permitTextBox, "رقم الإذن مستخدم");
					errorProvider.RightToLeft = true;
					return false;
				}
			}

			foreach (var errorProvider in errorProviders)
			{
				if (!string.IsNullOrEmpty(errorProvider.GetError(permitTextBox)))
				{
					errorProvider.SetError(permitTextBox, string.Empty);
				}
			}
			return true;		
		}

		public static bool ValidatePermitNumber(Form form, TextBox permitTextBox, List<ErrorProvider> errorProviders, string transactionType, int transactionId)
		{
			if (!string.IsNullOrEmpty(permitTextBox.Text.Trim()))
			{
				if (Business.CheckExisitngPermitNumber(permitTextBox.Text.Trim(), transactionType, transactionId))
				{
					var errorProvider = new ErrorProvider(form);
					errorProviders.Add(errorProvider);

					errorProvider.SetError(permitTextBox, "رقم الإذن مستخدم");
					errorProvider.RightToLeft = true;
					return false;
				}
			}

			foreach (var errorProvider in errorProviders)
			{
				if (!string.IsNullOrEmpty(errorProvider.GetError(permitTextBox)))
				{
					errorProvider.SetError(permitTextBox, string.Empty);
				}
			}
			return true;
		}

	}
}
